/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   Module Name          : com.yaodian100.ec.proposeproduct.service.impl.ProposeProductServiceImpl
   Module Description   :

   Date Created      : 2009/11/16
   Original Author   : ellen.li
   Team              : yaodian100
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   MODIFICATION HISTORY
   ------------------------------------------------------------------------------
   Date Modified       Modified by       Comments
   ------------------------------------------------------------------------------
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
package com.yaodian100.ec.proposeproduct.service.impl;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Serializable;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.transaction.annotation.Transactional;

import com.yaodian100.core.admin.entity.User;
import com.yaodian100.core.admin.entity.UserPermission;
import com.yaodian100.core.admin.entity.Permission.Type;
import com.yaodian100.core.admin.service.GroupService;
import com.yaodian100.core.admin.service.UserService;
import com.yaodian100.core.admin.utils.AdminHelper;
import com.yaodian100.core.common.dao.ObjectDao;
import com.yaodian100.core.common.dao.impl.CommonCriteria;
import com.yaodian100.core.common.dao.impl.CriteriaInRlike;
import com.yaodian100.core.common.exception.CoreException;
import com.yaodian100.core.common.service.impl.DomainServiceImpl;
import com.yaodian100.core.common.utils.ServerValue;
import com.yaodian100.core.menu.service.MenuService;
import com.yaodian100.ec.category.entity.Category;
import com.yaodian100.ec.category.service.CategoryService;
import com.yaodian100.ec.common.EcConstant;
import com.yaodian100.ec.image.entity.Image;
import com.yaodian100.ec.product.entity.Brand;
import com.yaodian100.ec.product.entity.Product;
import com.yaodian100.ec.product.entity.ProductCategory;
import com.yaodian100.ec.product.entity.Specification;
import com.yaodian100.ec.product.service.Product2Service;
import com.yaodian100.ec.product.service.ProductService;
import com.yaodian100.ec.proposeproduct.entity.ProposeProduct;
import com.yaodian100.ec.proposeproduct.entity.ProposeSpecification;
import com.yaodian100.ec.proposeproduct.service.ProposeProductService;
import com.yaodian100.ec.proposeproduct.service.ProposeSpecificationService;
import com.yaodian100.ec.supplierapply.entity.SupplierApply;
import com.yaodian100.ec.supplierapply.service.SupplierApplyService;

/**
 * @author ellen.li
 * 
 */
public class ProposeProductServiceImpl extends DomainServiceImpl<ProposeProduct> implements ProposeProductService {
	private ObjectDao<Image> imageDao;
	@Resource
	ProductService productService;
	@Resource
	Product2Service product2Service;
	@Resource
	ProposeSpecificationService proposeSpecificationService;
	@Resource
	CategoryService categoryService;
	@Resource
	private SupplierApplyService supplierApplyService;
	@Resource
	private UserService userService;
	@Resource
	private MenuService menuService;

	@Resource
	private GroupService groupService;

	private final String STRING_SYMBOL = "/";

	@Transactional(readOnly = false)
	public Map<String, Object> approveProduct(String[] proposeProductList) throws CoreException {
		Map<String, Object> resultList = new HashMap<String, Object>();
		int success = 0;
		int fail = 0;
		ArrayList<String> failReason = new ArrayList<String>();
		try {
			for (int i = 0; i < proposeProductList.length; i++) {
				ProposeProduct proposeProduct = getDao().get(proposeProductList[i]);
				// 審核時，若category_id is null，不允許審核
				if (0 != proposeProduct.getProposeCategoryId()) {
					// Category category = proposeProduct.getCategory();
					Category category = categoryService.get(new Long(proposeProduct.getProposeCategoryId()));
					if (null != category && !category.getIsDelete().equalsIgnoreCase("Y")) {
						// 新增至Product Table
						Product product = saveProduct(proposeProduct);
						if (StringUtils.isBlank(product.getApproveMemo())) {
							String specResult = checkSpec(proposeProduct.getId());
							if ("success".equals(specResult)) {
								List<Specification> productSpecs = saveSpec(proposeProduct.getId());
								product.setSpecifications(productSpecs);
								// productService.save(product);
								product2Service.saveOrUpdateProduct(product, true);
								proposeProduct.setStatus(1);
								this.update(proposeProduct);// 更新提報狀態
								// 提報審核核準後，若該商品有規格，需要同時新庫存資料
								// for (Specification spec : productSpecs) {
								// SpecificationInventory specificationInventory;
								// specificationInventory = new SpecificationInventory();
								// specificationInventory.setProductId(product.getProductId());
								// specificationInventory.setSpecId(spec.getSpecificationId());
								// specificationInventory.setAvailableInventory(0);
								// specificationInventory.setCancelInventory(0);
								// specificationInventory.setSoldInventory(0);
								// productService.saveSpecificationInventory(specificationInventory);
								// }

								// 提報審核核準後要新增一筆productcatory
								ProductCategory productCategory = new ProductCategory();
								productCategory.setProductId(product.getProductId());
								productCategory.setCategoryName(category.getName());
								productCategory.setCategoryPath(category.getCategoryPath());
								productCategory.setCategoryId(Long.parseLong(String.valueOf(proposeProduct.getProposeCategoryId())));
								productService.saveProductCategory(productCategory);

								success += 1;
							} else {
								failReason.add(specResult);
								fail += 1;
							}
						} else {
							failReason.add(product.getApproveMemo());
							fail += 1;
						}
					} else {
						failReason.add("商品分类未设定，不允許審核");
						fail += 1;
					}
				} else {
					failReason.add("商品分类未设定，不允許審核");
					fail += 1;
				}
			}
			resultList.put("successNumP", String.valueOf(success));
			// if (fail > 0) {
			resultList.put("failNumP", String.valueOf(fail));
			resultList.put("failReasonP", failReason);
			// }
		} catch (Exception e) {
			e.printStackTrace();
			logger.info("approveProduct error:{}" + e.fillInStackTrace());
		}
		return resultList;
	}

	/* check Spec */
	private String checkSpec(String productId) throws Exception {
		String flag = "";
		List<String> specSts = Arrays.asList("10", "20", "30", "40");
		CommonCriteria crit = new CommonCriteria();
		crit.addEq("proposeProductId", productId);
		List<ProposeSpecification> proposeSpecificationList = proposeSpecificationService.getList(0, -1, crit, null);
		for (int i = 0; i < proposeSpecificationList.size(); i++) {
			if (StringUtils.isBlank(proposeSpecificationList.get(i).getName())) {
				flag = "无[规格名称]:" + productId;
				return flag;
			}
			if (!specSts.contains(proposeSpecificationList.get(i).getSpecStatus())) {
				flag = "规格状态不符合:" + proposeSpecificationList.get(i).getSpecStatus();
				return flag;
			}
		}
		flag = "success";
		return flag;
	}

	private String converSupid(String supid) {
		String prefix = "000000";
		supid = prefix + supid;
		return supid.substring(supid.length() - 6, supid.length());
	}

	@Transactional(readOnly = false)
	public Map<String, Object> deleteProduct(String[] proposeProductList) throws CoreException {
		Map<String, Object> resultList = new HashMap<String, Object>();
		int success = 0;
		int fail = 0;
		ArrayList<String> failReason = new ArrayList<String>();
		for (int i = 0; i < proposeProductList.length; i++) {
			try {
				ProposeProduct proposeProduct = getDao().get(proposeProductList[i]);
				proposeProduct.setIsDelete("Y");
				save(proposeProduct);
				success++;
			} catch (Exception e) {
				logger.info("unapproveProduct error:{}" + e);
				fail++;
				failReason.add(proposeProductList[i] + "-" + e.getMessage());
			}
		}
		resultList.put("successNum", String.valueOf(success));
		if (fail > 0) {
			resultList.put("failNum", String.valueOf(fail));
			resultList.put("failReason", failReason);
		}
		return resultList;

	}

	/**
	 * @return the imageDao
	 */
	public ObjectDao<Image> getImageDao() {
		return imageDao;
	}

	public SupplierApplyService getSupplierApplyService() {
		return supplierApplyService;
	}

	// 置换档案中图档名
	public void replaceImageName(Map replaceNameMap, List replaceMapKeyList) {
		try {
			String originImgName = "";
			String replaceImgName = "";
			String proposeProductId = "";
			for (int i = 0; i < replaceMapKeyList.size(); i++) {
				originImgName = replaceNameMap.get(replaceMapKeyList.get(i)).toString().split(",")[0];
				proposeProductId = replaceNameMap.get(replaceMapKeyList.get(i)).toString().split(",")[1];
				replaceImgName = "";
				replaceImgName = "/PImg/" + proposeProductId.substring(0, 4) + "/" + proposeProductId + "/"
						+ replaceMapKeyList.get(i).toString();
				ProposeProduct proposeProduct = getDao().get(proposeProductId);
				if (null != proposeProduct) {
					String tmpBrief = productService.readFromFile(proposeProduct.getId(), "brief");
					String tmpDescription = productService.readFromFile(proposeProduct.getId(), "description");
					String tmpDetaiils = productService.readFromFile(proposeProduct.getId(), "details");
					tmpBrief = tmpBrief.replaceAll(originImgName, replaceImgName);
					tmpDescription = tmpDescription.replaceAll(originImgName, replaceImgName);
					tmpDetaiils = tmpDetaiils.replaceAll(originImgName, replaceImgName);
					saveToFile(proposeProduct.getId(), tmpBrief, "brief");
					saveToFile(proposeProduct.getId(), tmpDescription, "description");
					saveToFile(proposeProduct.getId(), tmpDetaiils, "details");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 儲存商品資料
	 * 
	 * @return
	 * @throws Exception
	 */
	private Product saveProduct(ProposeProduct proposeProduct) throws Exception {
		Product product = new Product();
		product.setProductId(proposeProduct.getId());
		if (StringUtils.isBlank(proposeProduct.getName())) {
			product.setApproveMemo("无[商品名称]:" + proposeProduct.getId());
			return product;
		}
		if (StringUtils.isBlank(String.valueOf(proposeProduct.getType()))) {
			product.setApproveMemo("无[商品型态]:" + proposeProduct.getId());
			return product;
		}

		if (StringUtils.isBlank(String.valueOf(proposeProduct.getCost()))) {
			product.setApproveMemo("无[成本]:" + proposeProduct.getId());
			return product;
		}
		if (2 != proposeProduct.getType()) {
			if (proposeProduct.getCost().compareTo(BigDecimal.ZERO) <= 0) {
				product.setApproveMemo("成本要大于0.00元:" + proposeProduct.getId());
				return product;
			}
		}
		if (StringUtils.isBlank(String.valueOf(proposeProduct.getListPrice()))) {
			product.setApproveMemo("无[建议售价]:" + proposeProduct.getId());
			return product;
		}
		if (proposeProduct.getListPrice().compareTo(BigDecimal.ZERO) <= 0) {
			product.setApproveMemo("建议售价大于0.00元:" + proposeProduct.getId());
			return product;
		}
		if (StringUtils.isBlank(proposeProduct.getSupplierId())) {
			product.setApproveMemo("无[供应商]:" + proposeProduct.getId());
			return product;
		}
		String supId = converSupid(proposeProduct.getSupplierId());
		SupplierApply sp = supplierApplyService.get(supId);
		if (null == sp) {
			product.setApproveMemo("[供应商]不存在:" + proposeProduct.getId());
			return product;
		}

		if (sp.getApproveStatus() == 0 || sp.getStatus() == 0) {
			product.setApproveMemo("[供应商]未被審核:" + proposeProduct.getId() + ",as:" + sp.getApproveStatus() + ","
					+ sp.getStatus());
			return product;
		}

		if ("Y".equals(proposeProduct.getIsScm())
				&& (StringUtils.isBlank(sp.getShipAddr()) || StringUtils.isBlank(sp.getShipProvince())
						|| StringUtils.isBlank(sp.getShipCity()) || StringUtils.isBlank(sp.getShipArea())
						|| StringUtils.isBlank(sp.getShipContact()) || StringUtils.isBlank(sp.getShipZip()) || StringUtils
						.isBlank(sp.getShipPhone()))) {
			product.setApproveMemo("[供应商]请先设定供应商的发货资料:" + proposeProduct.getSupplierId());
			return product;
		}
		if ("Y".equals(proposeProduct.getIsScm()) && "N".equals(sp.getIsScm())) {
			product.setApproveMemo("[供应商]供应商不支援转单服务 :" + proposeProduct.getSupplierId());
			return product;
		}
		if (StringUtils.isBlank(proposeProduct.getPmId())) {
			product.setApproveMemo("无[负责PM]:" + proposeProduct.getId());
			return product;
		}
		if (StringUtils.isBlank(String.valueOf(proposeProduct.getLength()))) {
			product.setApproveMemo("无[长]:" + proposeProduct.getLength());
			return product;
		}
		if (StringUtils.isBlank(String.valueOf(proposeProduct.getWidth()))) {
			product.setApproveMemo("无[宽]:" + proposeProduct.getWidth());
			return product;
		}
		if (StringUtils.isBlank(String.valueOf(proposeProduct.getHeight()))) {
			product.setApproveMemo("无[高]:" + proposeProduct.getHeight());
			return product;
		}
		if (StringUtils.isBlank(String.valueOf(proposeProduct.getWeight()))) {
			product.setApproveMemo("无[重量]:" + proposeProduct.getWeight());
			return product;
		}
		if (StringUtils.isBlank(proposeProduct.getUnit())) {
			product.setApproveMemo("无[销售单位]:" + proposeProduct.getUnit());
			return product;
		}
		if (StringUtils.isBlank(String.valueOf(proposeProduct.getVolume()))) {
			product.setApproveMemo("无[体积]:" + proposeProduct.getVolume());
			return product;
		}
		if (StringUtils.isBlank(String.valueOf(proposeProduct.getProposeCategoryId()))
				|| proposeProduct.getProposeCategoryId() == 0) {
			product.setApproveMemo("无[分馆/目录]:" + proposeProduct.getProposeCategoryId());
			return product;
		}
		Category category = categoryService.get(new Long(proposeProduct.getProposeCategoryId()));
		if (null == category) {
			product.setApproveMemo("无[分馆/目录]:" + proposeProduct.getProposeCategoryId());
			return product;
		}
		if (category.getIsDelete().equals("Y")) {
			product.setApproveMemo("[分馆/目录]已刪除:" + proposeProduct.getProposeCategoryId());
			return product;
		}
		if (StringUtils.isBlank(category.getRootId())) {
			product.setApproveMemo("无[分馆/目录]:" + proposeProduct.getProposeCategoryId());
			return product;
		}
		User user = userService.get(proposeProduct.getPmId());

		if (null == user) {
			product.setApproveMemo("PM不存在:" + proposeProduct.getPmId());
			return product;
		}
		if (!user.getStatus().equals(User.STATUS_ACTIVE)) {
			product.setApproveMemo("PM已停用:" + proposeProduct.getPmId());
			return product;
		}
		if (!user.getGroupName().startsWith("A-20")) {
			product.setApproveMemo("PM 不为商品处[ " + user.getGroupName() + "]:" + proposeProduct.getPmId());
			return product;
		}
		if (StringUtils.isNotBlank(proposeProduct.getIsScm())) {
			product.setIsScm(proposeProduct.getIsScm());
		} else {
			product.setIsScm("N");
		}
		List<String> ems = Arrays.asList("EM01", "EM02", "EM03", "EM04", "EM99");
		if (!ems.contains(proposeProduct.getExpireManage())) {
			product.setApproveMemo("效期管理分类不符合:" + proposeProduct.getExpireManage());
			return product;
		}
		if ("0".equals(proposeProduct.getIsAntiUnpacking()) || StringUtils.isBlank(proposeProduct.getIsAntiUnpacking())) {
			product.setIsAntiUnpacking("N");
		} else {
			product.setIsAntiUnpacking(proposeProduct.getIsAntiUnpacking());
		}
		List<String> tts = Arrays.asList("1", "2", "3");
		if (!tts.contains(String.valueOf(proposeProduct.getTemperatureType()))) {
			product.setApproveMemo("物流仓储及运送温层类别不符合:" + proposeProduct.getTemperatureType());
			return product;
		}
		List<String> pts = Arrays.asList("0", "1", "2", "3", "4", "5", "7");
		if (!pts.contains(String.valueOf(proposeProduct.getType()))) {
			product.setApproveMemo("商品型态不符合:" + proposeProduct.getType());
			return product;
		}
		/*
		 * 一般：po、bo(可訂)、scm(可訂) 贈品、配件：po、bo、scm 預購：bo(可訂)、scm(可訂)
		 */
		if (0 == proposeProduct.getType() || 1 == proposeProduct.getType()) {
			if (Arrays.asList("B", "S").contains(proposeProduct.getIsScm()) && "Y".equals(proposeProduct.getUseDeliveryDay())
					&& proposeProduct.getDeliveryDay() == 0) {
				product.setApproveMemo("采购模式为BO、SCM时订制商品天数不可为0:" + proposeProduct.getId());
				return product;
			}
		}

		// 台湾馆供应商检核
		if (Arrays.asList("010000", "010001").contains(proposeProduct.getSupplierId())) {
			if (StringUtils.isBlank(proposeProduct.getTwSupplierId())) {
				product.setApproveMemo("供应商为万美(010000 or 010001)时，台湾馆供应商为必填:" + proposeProduct.getId());
				return product;
			}
		}
		if (StringUtils.isNotBlank(proposeProduct.getTwSupplierId())) {
			SupplierApply twSp = supplierApplyService.get(proposeProduct.getTwSupplierId());
			if (null == twSp) {
				product.setApproveMemo("[台湾馆供应商]不存在:" + proposeProduct.getId());
				return product;
			}
			if (twSp.getApproveStatus() == 0 || twSp.getStatus() == 0) {
				product.setApproveMemo("[台湾馆供应商]未被審核:" + proposeProduct.getId() + ",as:" + twSp.getApproveStatus() + ","
						+ twSp.getStatus());
				return product;
			}
		}
		product.setTwSupplierId(proposeProduct.getTwSupplierId());
		product.setExpireManage(proposeProduct.getExpireManage());
		product.setOrganizationPath(user.getGroupName());
		product.setSupplierId(supId);
		product.setProductType(Integer.valueOf(proposeProduct.getType()));
		product.setIsVirtual(StringUtils.isBlank(proposeProduct.getIsVirtual()) ? "N" : proposeProduct.getIsVirtual());
		product.setProductName(proposeProduct.getName().trim());
		product.setProductCost(proposeProduct.getCost());
		product.setListPrice(proposeProduct.getListPrice());
		product.setPrpsShelfPrice(proposeProduct.getPrpsShelfPrice());
		if (StringUtils.isNotBlank(proposeProduct.getBrand())) {
			CommonCriteria crit = new CommonCriteria();
			crit.addEq("brandId", proposeProduct.getBrand());
			if (productService.getBrandList(0, -1, crit, null).size() > 0) {
				product.setProductBrand(productService.getBrandList(0, -1, crit, null).get(0).getBrandId());
			} else {
				product.setProductBrand("");
			}
		} else {
			product.setProductBrand("");
		}
		product.setTaxClass(proposeProduct.getTaxClass());
		product.setLeadTime(proposeProduct.getLeadTime());
		product.setIsDeliverTolerance(StringUtils.isBlank(proposeProduct.getIsDeliverdTolerance()) ? "N" : proposeProduct
				.getIsDeliverdTolerance());
		product.setProductLength(proposeProduct.getLength());
		product.setProductWidth(proposeProduct.getWidth());
		product.setProductHeight(proposeProduct.getHeight());
		product.setProductWeight(proposeProduct.getWeight());
		product.setProductVolume(proposeProduct.getVolume());
		product.setProductUnit(proposeProduct.getUnit());
		product.setMinPOQuantity(proposeProduct.getMinPoQuantity());
		product.setMaxPOQuantity(proposeProduct.getMaxPoQuantity());
		product.setTemperatureType(proposeProduct.getTemperatureType());
		product.setIsReturnPickup(StringUtils.isBlank(proposeProduct.getIsReturnPickup()) ? "Y" : proposeProduct
				.getIsReturnPickup());
		product.setIsReturnable(StringUtils.isBlank(proposeProduct.getIsReturnable()) ? "Y" : proposeProduct
				.getIsReturnable());
		product.setPmID(proposeProduct.getPmId());
		product.setProductMemo(proposeProduct.getMeno());
		product.setProductKeyword(proposeProduct.getProductKeyword());
		product.setDeliverGeoRange(proposeProduct.getDeliverGeoRange());
		product.setIsDelete("N");
		product.setProductStatus("PRD500");// ?
		product.setShowSeq(1);
		product.setMainCategoryID(proposeProduct.getProposeCategoryId());// x
		product.setMainCategoryPath(category.getCategoryPath());
		product.setUseDeliveryDay(proposeProduct.getUseDeliveryDay());
		product.setDeliveryDay(proposeProduct.getDeliveryDay());
		// // 将特色去除html code存入db
		if (StringUtils.isNotBlank(productService.readFromFile(proposeProduct.getId(), "brief"))) {
			product.setProductBrief(StringUtils.substring(com.yaodian100.core.common.utils.StringUtils.removeHTML(
					productService.readFromFile(proposeProduct.getId(), "brief"), true), 0, 50));
		}
		// 将活动说明去除html code存入db
		if (StringUtils.isNotBlank(productService.readFromFile(proposeProduct.getId(), "description"))) {
			product.setProductDescription(StringUtils.substring(com.yaodian100.core.common.utils.StringUtils.removeHTML(
					productService.readFromFile(proposeProduct.getId(), "description"), true), 0, 50));
		}
		// 将活动规格内容去除html code存入db
		if (StringUtils.isNotBlank(productService.readFromFile(proposeProduct.getId(), "details"))) {
			product.setProductDetails(StringUtils.substring(com.yaodian100.core.common.utils.StringUtils.removeHTML(
					productService.readFromFile(proposeProduct.getId(), "details"), true), 0, 50));
		}
		// 长形图/方形图(S:方形圖,R:長形圖)
		product.setGraphicType(proposeProduct.getGraphicType());
		// 雙屬性部份
		product.setMainCodeLabel(proposeProduct.getMainCodeLabel());
		product.setMainSpecName(proposeProduct.getMainSpecName());
		product.setSecondCodeLabel(proposeProduct.getSecondCodeLabel());
		product.setSecondSpecName(proposeProduct.getSecondSpecName());

		product.setDataSrc("PM");
		product.setStepFlag(2);

		// 退换货条件
		product.setReturnRule(proposeProduct.getReturnRule());
		return product;
	}

	/* 新增Spec */
	private List<Specification> saveSpec(String productId) throws Exception {
		CommonCriteria crit = new CommonCriteria();
		crit.addEq("proposeProductId", productId);
		List<ProposeSpecification> proposeSpecificationList = proposeSpecificationService.getList(0, -1, crit, null);

		List<Specification> specList = new ArrayList<Specification>();

		for (int i = 0; i < proposeSpecificationList.size(); i++) {
			Specification specification = new Specification();
			specification.setSpecificationName(proposeSpecificationList.get(i).getName().trim());
			specification.setBarCode(proposeSpecificationList.get(i).getBarCode());
			specification.setSpecCode(proposeSpecificationList.get(i).getSpecCode());
			specification.setReverseInventory(proposeSpecificationList.get(i).getReverseInventory());
			specification.setMaxCountPerOrder(proposeSpecificationList.get(i).getMaxCountPerOrder());
			specification.setMinCountPerOrder(proposeSpecificationList.get(i).getMinCountPerOrder());
			specification.setSafeInventory(proposeSpecificationList.get(i).getSafeInventory());
			specification.setIsDelete("N");
			specification.setShowSeq(1);
			specification.setProductId(productId);
			specification.setSpecStatus(proposeSpecificationList.get(i).getSpecStatus());

			// productService.saveSpecification(specification);
			specList.add(specification);
		}
		return specList;
	}

	/** 將brief,description,details 儲存成檔案 */
	public void saveToFile(String productId, String context, String contextType) throws CoreException {
		try {
			String filenamePath = ServerValue.getMidProdHtmlPath();
			FileUtils.forceMkdir(new File(filenamePath + File.separator + productId.substring(0, 4) + File.separator));
			FileUtils.forceMkdir(new File(filenamePath + File.separator + productId.substring(0, 4) + File.separator
					+ productId + File.separator));
			File file = new File(filenamePath + File.separator + productId.substring(0, 4) + File.separator + productId
					+ File.separator + productId + "_" + contextType + ".txt");
			FileWriter fw;
			fw = new FileWriter(file);
			context = StringUtils
					.remove(
							context,
							"<link href=\"http://www.yaodian100.com/css/yaodian100_pages_v2.css\" media=\"screen,print\" rel=\"stylesheet\" type=\"text/css\" />");
			context = StringUtils
					.remove(
							context,
							"<link href=\"http://www.yaodian100.com/css/pdpagese_v2.css\" media=\"screen,print\" rel=\"stylesheet\" type=\"text/css\" />");
			fw.write(new String(context.getBytes(), "UTF-8"));
			fw.close();
		} catch (IOException e) {
			logger.info("ProposeProductApproveAction_saveToFile error :{}", e.toString());
		}
	}

	/**
	 * @param imageDao the imageDao to set
	 */
	public void setImageDao(ObjectDao<Image> imageDao) {
		this.imageDao = imageDao;
	}

	public void setSupplierApplyService(SupplierApplyService supplierApplyService) {
		this.supplierApplyService = supplierApplyService;
	}

	@Transactional(readOnly = false)
	public Map<String, Object> unapproveProduct(String[] proposeProductList) throws CoreException {
		Map<String, Object> resultList = new HashMap<String, Object>();
		int success = 0;
		int fail = 0;
		ArrayList<String> failReason = new ArrayList<String>();

		for (int i = 0; i < proposeProductList.length; i++) {
			try {
				ProposeProduct proposeProduct = getDao().get(proposeProductList[i]);
				proposeProduct.setIsDelete("N");
				proposeProduct.setStatus(0);
				save(proposeProduct);
				success++;
			} catch (Exception e) {
				logger.info("unapproveProduct error:{}" + e);
				fail++;
				failReason.add(proposeProductList[i] + "-" + e.getMessage());
			}
		}
		resultList.put("successNum", String.valueOf(success));
		if (fail > 0) {
			resultList.put("failNum", String.valueOf(fail));
			resultList.put("failReason", failReason);
		}
		return resultList;

	}

	@Override
	@Transactional(readOnly = false)
	public Map<String, Object> uploadExcelAndSaveAsProduct(List<List> excelLists, String batchId, String supplier,
			String supplierTW) throws CoreException {
		Map<String, Object> result = uploadExcelDuoSpec(excelLists, batchId, supplier, supplierTW);
		Map<String, Object> productResult = new HashMap<String, Object>(result);
		List<String> readyToProduct = (List<String>) result.get("readyToProduct");
		if (readyToProduct.size() > 0) {

			productResult.putAll(approveProduct(readyToProduct.toArray(new String[readyToProduct.size()])));
		}

		return productResult;
	}

	private Map<String, Object> uploadExcelDuoSpec(List<List> excelLists, String batchId, String supplier,
			String supplierTW) throws CoreException {
		Map<String, Object> resultList = new HashMap<String, Object>();
		int countTotal = 0;
		int success = 0;
		int fail = 0;
		int count = 0;
		ArrayList<String> failReason = new ArrayList<String>();
		Hashtable<String, String> checkName = new Hashtable<String, String>();// 确认下一笔名称是否相同
		List<String> defaultTps = Arrays.asList("@", "＠", "");
		List<String> ynTps = Arrays.asList("Y", "N");
		List<String> scmTps = Arrays.asList("Y", "N", "B");// 采购型态(scm、po、bo)
		List<String> grapTps = Arrays.asList("R", "S");// 长形图/方形图
		List<String> products = new ArrayList<String>();
		Hashtable<String, String> setCover = new Hashtable<String, String>();
		;
		try {
			countTotal = excelLists.size();
			for (List<String> list : excelLists) {
				count++;
				try {
					if (list.size() > 0) {

						ProposeProduct proposeProduct = new ProposeProduct();
						String thisProductName = "";
						if (checkName.containsKey(list.get(0)) == false) {// 判断是否在hashtable已有商品名称
							// A 0 大标(商品名称)
							if (null != proposeProduct) {
								thisProductName = list.get(0).trim();
								if (defaultTps.contains(thisProductName)) {
									failReason.add("第" + count + "笔资料-大标(商品名称)：不可为空<BR/>");
									proposeProduct = null;
									fail += 1;
								} else if (thisProductName.length() > 44) {
									failReason.add("第" + count + "笔资料-大标(商品名称)：" + thisProductName + " 长度不可超过44字<BR/>");
									proposeProduct = null;
									fail += 1;
								} else {
									proposeProduct.setName(thisProductName);
									checkName.put(thisProductName, thisProductName);
								}
							}
							// B 1 PM所属单位
							if (null != proposeProduct) {
								proposeProduct.setOrganizationPath("");
							}
							// C 2 主要負責PM
							if (null != proposeProduct) {
								String pm = list.get(2).trim();
								if (defaultTps.contains(pm)) {
									failReason.add("第" + count + "笔资料-主要負責PM：不可为空<BR/>");
									proposeProduct = null;
									fail += 1;
								} else {
									User user = userService.get(pm);

									if (null == user) {
										failReason.add("第" + count + "笔资料-主要負責PM：" + pm + " PM不存在<BR/>");
										proposeProduct = null;
										fail += 1;
									} else if (!user.getStatus().equals(User.STATUS_ACTIVE)) {
										failReason.add("第" + count + "笔资料-主要負責PM：" + pm + " PM已停用<BR/>");
										proposeProduct = null;
										fail += 1;
									} else if (!user.getGroupName().startsWith("A-20")) {
										failReason.add("第" + count + "笔资料-主要負責PM：" + pm + " PM 不为商品处<BR/>");
										proposeProduct = null;
										fail += 1;
									} else {
										proposeProduct.setPmId(pm.toLowerCase());
									}

								}
							}

							// D 3 采购模式
							if (null != proposeProduct) {
								String scmType = list.get(3).trim().toUpperCase();
								if (scmTps.contains(scmType)) {
									proposeProduct.setIsScm(scmType);
								} else {
									failReason.add("第" + count + "笔资料-采购模式： 格式错误<BR/>");
									proposeProduct = null;
									fail += 1;
								}
							}

							// E 4 商品型態
							if (null != proposeProduct) {
								String productType = list.get(4).trim();
								if (defaultTps.contains(productType)) {
									failReason.add("第" + count + "笔资料-商品型態：不可为空<BR/>");
									proposeProduct = null;
									fail += 1;
								} else {
									if (menuService.get("Product.Type").getOptions().containsKey(productType)) {
										proposeProduct.setType(Integer.parseInt(productType));
									} else {
										failReason.add("第" + count + "笔资料-商品型態：" + productType + " 商品形态不正确<BR/>");
										proposeProduct = null;
										fail += 1;

									}
								}
							}
							// 预购商品的采购模式不可为入仓
							if (null != proposeProduct && 1 == proposeProduct.getType()
									&& "N".equalsIgnoreCase(proposeProduct.getIsScm())) {
								failReason.add("第" + count + "笔资料-预购商品的采购模式不可为入仓<BR/>");
								proposeProduct = null;
								fail += 1;
							}

							// 供應商檢查
							if (null != proposeProduct) {
								String supId = converSupid(supplier);
								SupplierApply sp = supplierApplyService.get(supId);
								if (null != sp) {
									if ("Y".equals(proposeProduct.getIsScm())
											&& (StringUtils.isBlank(sp.getShipAddr()) || StringUtils.isBlank(sp.getShipProvince())
													|| StringUtils.isBlank(sp.getShipCity()) || StringUtils.isBlank(sp.getShipArea())
													|| StringUtils.isBlank(sp.getShipContact()) || StringUtils.isBlank(sp.getShipZip()) || StringUtils
													.isBlank(sp.getShipPhone()))) {
										failReason.add("第" + count + "笔资料-供应商的发货资料不正确<BR/>");
										proposeProduct = null;
										fail += 1;
									}
								} else if ("Y".equals(proposeProduct.getIsScm()) && "N".equals(sp.getIsScm())) {
									failReason.add("第" + count + "笔资料-供应商不支援转单服务<BR/>");
									proposeProduct = null;
									fail += 1;
								} else {

									failReason.add("第" + count + "笔资料-供应商不正确<BR/>");
									proposeProduct = null;
									fail += 1;
								}
							}

							// F 5 订制商品
							if (null != proposeProduct) {
								String customP = list.get(5).trim();
								if (defaultTps.contains(customP)) {
									failReason.add("第" + count + "笔资料-订制商品：不可为空<BR/>");
									proposeProduct = null;
									fail += 1;
								} else if (ynTps.contains(customP)) {
									proposeProduct.setUseDeliveryDay(customP.toUpperCase());
								} else {
									failReason.add("第" + count + "笔资料-订制商品：" + customP + " 格式错误<BR/>");
									proposeProduct = null;
									fail += 1;
								}
							}

							// G 6 订制所需时间
							if (null != proposeProduct) {
								String customTime = list.get(6).trim();
								if ("Y".equals(proposeProduct.getUseDeliveryDay())) {
									if (defaultTps.contains(customTime)) {
										failReason.add("第" + count + "笔资料-订制所需时间：使用订制商品时不可为空<BR/>");
										proposeProduct = null;
										fail += 1;
									} else if (NumberUtils.isNumber(customTime)) {
										proposeProduct.setDeliveryDay(Integer.parseInt(customTime));
									} else {
										failReason.add("第" + count + "笔资料-订制所需时间：" + customTime + " 格式错误<BR/>");
										proposeProduct = null;
										fail += 1;
									}
								} else {// 订制商品不使用时自动为0
									proposeProduct.setDeliveryDay(0);
								}
							}

							// H 7 预购出货日 yyyy-MM-dd
							if (null != proposeProduct) {
								String expectShipDate = list.get(7).trim();
								SimpleDateFormat dfExpectDate = new SimpleDateFormat("yyyy-MM-dd");

								if (defaultTps.contains(expectShipDate)) {
									if (proposeProduct.getType() == 1) {// 預購商品必填
										failReason.add("第" + count + "笔资料-预购出货日：不可为空<BR/>");
										proposeProduct = null;
										fail += 1;
									}
								} else {
									try {
										proposeProduct.setExpectShipDate(dfExpectDate.parse(expectShipDate));
									} catch (Exception e) {
										failReason.add("第" + count + "笔资料-预购出货日：" + expectShipDate + " 格式错误<BR/>");
										proposeProduct = null;
										fail += 1;
									}
								}
							}

							// I 8 稅率類別
							if (null != proposeProduct) {
								String taxClass = list.get(8).trim();
								if (defaultTps.contains(taxClass)) {
									failReason.add("第" + count + "笔资料-稅率類別：不可为空<BR/>");
									proposeProduct = null;
									fail += 1;
								} else {
									try {
										proposeProduct.setTaxClass(new BigDecimal(taxClass).setScale(2, BigDecimal.ROUND_HALF_UP)
												.toString());
									} catch (Exception e) {
										failReason.add("第" + count + "笔资料-稅率類別：" + taxClass + " 格式错误<BR/>");
										fail += 1;
										proposeProduct = null;
									}
								}
							}

							// J 9 效期管理分类 ExpireManage.Attribute
							if (null != proposeProduct) {
								String expireManager = list.get(9).trim().toUpperCase();
								if (defaultTps.contains(expireManager)) {
									failReason.add("第" + count + "笔资料-效期管理分类：不可为空<BR/>");
									proposeProduct = null;
									fail += 1;
								} else {
									if (menuService.get("ExpireManage.Attribute").getOptions().containsKey(expireManager)) {
										proposeProduct.setExpireManage(expireManager);
									} else {
										failReason.add("第" + count + "笔资料-效期管理分类：" + expireManager + " 格式错误<BR/>");
										proposeProduct = null;
										fail += 1;
									}
								}
							}

							// K 10 物流温层类别
							if (null != proposeProduct) {
								String temperatureType = list.get(10).trim();
								List<String> tempTypeList = Arrays.asList("1", "2", "3");
								if (defaultTps.contains(temperatureType)) {
									failReason.add("第" + count + "笔资料-物流温层类别：不可为空<BR/>");
									proposeProduct = null;
									fail += 1;
								} else {
									if (tempTypeList.contains(temperatureType)) {
										proposeProduct.setTemperatureType(Integer.parseInt(temperatureType));
									} else {
										failReason.add("第" + count + "笔资料-物流温层类别：" + temperatureType + " 格式错误<BR/>");
										proposeProduct = null;
										fail += 1;
									}

								}
							}

							// L 11 是否需要防拆包材
							if (null != proposeProduct) {
								String antiUnpacking = list.get(11).trim().toUpperCase();
								if (defaultTps.contains(antiUnpacking)) {
									failReason.add("第" + count + "笔资料-是否需要防拆包材：不可为空<BR/>");
									proposeProduct = null;
									fail += 1;
								} else {
									if (ynTps.contains(antiUnpacking)) {
										proposeProduct.setIsAntiUnpacking(antiUnpacking);
									} else {
										failReason.add("第" + count + "笔资料-是否需要防拆包材：" + antiUnpacking + " 格式错误<BR/>");
										proposeProduct = null;
										fail += 1;
									}

								}
							}

							// M 12 长(cm)
							if (null != proposeProduct) {
								String length = list.get(12).trim();
								if (defaultTps.contains(length)) {
									failReason.add("第" + count + "笔资料-长(cm)：不可为空<BR/>");
									proposeProduct = null;
									fail += 1;
								} else {
									try {
										proposeProduct.setLength(new BigDecimal(length).setScale(2, BigDecimal.ROUND_HALF_UP));
									} catch (Exception e) {
										failReason.add("第" + count + "笔资料-长(cm)：" + length + " 格式错误<BR/>");
										proposeProduct = null;
										fail += 1;
									}
								}
							}

							// N 13 宽(cm)
							if (null != proposeProduct) {
								String width = list.get(13).trim();
								if (defaultTps.contains(width)) {
									failReason.add("第" + count + "笔资料-宽(cm)：不可为空<BR/>");
									proposeProduct = null;
									fail += 1;
								} else {
									try {
										proposeProduct.setWidth(new BigDecimal(width).setScale(2, BigDecimal.ROUND_HALF_UP));
									} catch (Exception e) {
										failReason.add("第" + count + "笔资料-宽(cm)：" + width + " 格式错误<BR/>");
										proposeProduct = null;
										fail += 1;
									}
								}
							}

							// O 14 高(cm)
							if (null != proposeProduct) {
								String height = list.get(14).trim();
								if (defaultTps.contains(height)) {
									failReason.add("第" + count + "笔资料-高(cm)：不可为空<BR/>");
									proposeProduct = null;
									fail += 1;
								} else {
									try {
										proposeProduct.setHeight(new BigDecimal(height).setScale(2, BigDecimal.ROUND_HALF_UP));
									} catch (Exception e) {
										failReason.add("第" + count + "笔资料-高(cm)：" + height + " 格式错误<BR/>");
										proposeProduct = null;
										fail += 1;
									}
								}
							}

							// P 15 重量(g)
							if (null != proposeProduct) {
								String weight = list.get(15).trim();
								if (defaultTps.contains(weight)) {
									failReason.add("第" + count + "笔资料-重量(g)：不可为空<BR/>");
									proposeProduct = null;
									fail += 1;
								} else {
									try {
										proposeProduct.setWeight(new BigDecimal(weight).setScale(2, BigDecimal.ROUND_HALF_UP));
									} catch (Exception e) {
										failReason.add("第" + count + "笔资料-重量(g)：" + weight + " 格式错误<BR/>");
										proposeProduct = null;
										fail += 1;
									}
								}

							}

							// Q 16 销售单位
							if (null != proposeProduct) {
								String saleUnit = list.get(16).trim();
								if (defaultTps.contains(saleUnit)) {
									failReason.add("第" + count + "笔资料-销售单位：不可为空<BR/>");
									proposeProduct = null;
									fail += 1;
								} else {
									proposeProduct.setUnit(saleUnit);
								}
							}

							// R 17 分类
							if (null != proposeProduct) {
								String categoryStr = list.get(17).trim();
								if (defaultTps.contains(categoryStr)) {
									failReason.add("第" + count + "笔资料-分类：不可为空<BR/>");
									proposeProduct = null;
									fail += 1;
								} else {

									Category category = categoryService.get(new Long(categoryStr));

									if (null == category) {
										failReason.add("第" + count + "笔资料-分类：" + categoryStr + " 分类不存在<BR/>");
										proposeProduct = null;
										fail += 1;

									} else if (category.getIsDelete().equals("Y")) {
										failReason.add("第" + count + "笔资料-分类：" + categoryStr + " 分类已删除<BR/>");
										proposeProduct = null;
										fail += 1;

									} else if (StringUtils.isBlank(category.getRootId())) {
										failReason.add("第" + count + "笔资料-分类：" + categoryStr + " 分类不存在<BR/>");
										proposeProduct = null;
										fail += 1;

									} else {
										proposeProduct.setProposeCategoryId(Integer.parseInt(categoryStr));
									}

								}
							}

							// S 18 品牌
							if (null != proposeProduct) {
								String brandStr = list.get(18).trim();
								if (defaultTps.contains(brandStr)) {
									failReason.add("第" + count + "笔资料-品牌：不可为空<BR/>");
									proposeProduct = null;
									fail += 1;
								} else {
									try {
										Brand brand = product2Service.getBrandByBrandId(brandStr);
										if (null != brand) {
											proposeProduct.setBrand(brandStr);
										} else {
											failReason.add("第" + count + "笔资料-品牌：" + brandStr + " 品牌不存在<BR/>");
											proposeProduct = null;
											fail += 1;
										}
									} catch (Exception e) {
										e.printStackTrace();
										failReason.add("第" + count + "笔资料-品牌：" + brandStr + " 品牌不存在<BR/>");
										proposeProduct = null;
										fail += 1;
									}
								}
							}

							// T 19 商品成本
							if (null != proposeProduct) {
								String cost = list.get(19).trim();
								if (defaultTps.contains(cost)) {
									failReason.add("第" + count + "笔资料-商品成本：不可为空<BR/>");
									proposeProduct = null;
									fail += 1;
								} else {
									BigDecimal testCost = new BigDecimal("0");
									try {
										testCost = new BigDecimal(cost).setScale(2, BigDecimal.ROUND_HALF_UP);
									} catch (Exception e) {
										failReason.add("第" + count + "笔资料-商品成本：" + cost + " 格式错误<BR/>");
										proposeProduct = null;
										fail += 1;
									}
									if (testCost.compareTo(BigDecimal.ZERO) > 0) {
										proposeProduct.setCost(testCost);
									} else {
										failReason.add("第" + count + "笔资料-商品成本：" + cost + " 成本需大于0<BR/>");
										proposeProduct = null;
										fail += 1;
									}

								}
							}

							// U 20 提报耀点价
							if (null != proposeProduct) {
								String prpsSheldPrice = list.get(20).trim();
								if (defaultTps.contains(prpsSheldPrice)) {
									failReason.add("第" + count + "笔资料-提报耀点价：不可为空<BR/>");
									proposeProduct = null;
									fail += 1;
								} else {
									try {
										proposeProduct.setPrpsShelfPrice(new BigDecimal(prpsSheldPrice).setScale(2,
												BigDecimal.ROUND_HALF_UP));
									} catch (Exception e) {
										failReason.add("第" + count + "笔资料-提报耀点价：" + prpsSheldPrice + " 格式错误<BR/>");
										proposeProduct = null;
										fail += 1;
									}
								}

							}

							// V 21 厂商建议价(市价)
							if (null != proposeProduct) {
								String listPrice = list.get(21).trim();

								if (defaultTps.contains(listPrice)) {
									failReason.add("第" + count + "笔资料-厂商建议价(市价)：不可为空<BR/>");
									proposeProduct = null;
									fail += 1;
								} else {
									BigDecimal testCost = new BigDecimal("0");
									try {
										testCost = new BigDecimal(listPrice).setScale(2, BigDecimal.ROUND_HALF_UP);
									} catch (Exception e) {
										failReason.add("第" + count + "笔资料-厂商建议价(市价)：" + listPrice + " 格式错误<BR/>");
										proposeProduct = null;
										fail += 1;
									}
									if (testCost.compareTo(BigDecimal.ZERO) > 0) {
										proposeProduct.setListPrice(testCost);
									} else {
										failReason.add("第" + count + "笔资料-厂商建议价(市价)：" + listPrice + " 成本需大于0<BR/>");
										proposeProduct = null;
										fail += 1;
									}

								}

							}

							// W 22 关键字
							if (null != proposeProduct) {
								String keywords = list.get(22).trim();
								if (defaultTps.contains(keywords)) {
									proposeProduct.setProductKeyword("");
								} else {
									proposeProduct.setProductKeyword(keywords);
								}
							}

							// X 23 商品特色
							if (null != proposeProduct) {
								if (defaultTps.contains(list.get(23).trim())) {
									failReason.add("第" + count + "笔资料-商品特色：不可为空<BR/>");
									proposeProduct = null;
									fail += 1;
								}
							}

							// Y 24 商品描述
							if (null != proposeProduct) {
								if (defaultTps.contains(list.get(24).trim())) {
									failReason.add("第" + count + "笔资料-商品描述：不可为空<BR/>");
									proposeProduct = null;
									fail += 1;
								}
							}

							// Z 25 规格细节
							if (null != proposeProduct) {
								if (defaultTps.contains(list.get(25).trim())) {
									failReason.add("第" + count + "笔资料-规格细节：不可为空<BR/>");
									proposeProduct = null;
									fail += 1;
								}
							}

							// AB 27 商品页为长形图/正方图?
							if (null != proposeProduct) {
								String prodImgType = list.get(27).trim();
								if (!grapTps.contains(prodImgType)) {
									proposeProduct.setGraphicType("S");
								} else {
									proposeProduct.setGraphicType(prodImgType);
								}
							}

							// AC 28 规格图(40*40)
							// if (null != proposeProduct) {
							// String iconPics = list.get(28).trim();
							// if (defaultTps.contains(iconPics)) {
							// failReason.add("第" + count + "笔资料-规格图(40*40)：不可为空<BR/>");
							// proposeProduct = null;
							// fail += 1;
							// } else {
							// proposeProduct.setProposeProductIconPic(iconPics);
							// }
							// }

							// AD 29 正方形主图(400*400)，改由spec去做
							if (null != proposeProduct) {
								proposeProduct.setProposeProductMainImg("");

							}

							// AE 30 长方形主图（400*530)，改由spec去做
							if (null != proposeProduct) {
								proposeProduct.setProposeProductRMainImg("");
							}
							// AF 31 「商品描述」/「规格细节」区用图
							if (null != proposeProduct) {
								String productDescPIcs = list.get(31).trim();
								if (defaultTps.contains(productDescPIcs)) {
									// failReason.add("第" + count + "笔资料-「商品描述」/「规格细节」区用图：不可为空<BR/>");
									// proposeProduct = null;
									// fail += 1;
									proposeProduct.setProposeProductDescImg(productDescPIcs);
								} else {
									proposeProduct.setProposeProductDescImg(productDescPIcs);
								}
							}
							// AG 32 「商品图片」区用图(520x620)
							if (null != proposeProduct) {
								String productPics = list.get(32).trim();
								if (defaultTps.contains(productPics)) {
									// failReason.add("第" + count + "笔资料-「商品图片」区用图(520x620)：不可为空<BR/>");
									// proposeProduct = null;
									// fail += 1;
									proposeProduct.setProposeProductPrdtImg("");
								} else {
									proposeProduct.setProposeProductPrdtImg(productPics);
								}
							}

							// AH 33 主规格名
							if (null != proposeProduct) {
								String mainSpecLabel = list.get(33).trim();

								if (!defaultTps.contains(mainSpecLabel)) {
									String tempStr = mainSpecLabel;
									if (tempStr.startsWith("3")) {
										tempStr = StringUtils.split(tempStr, "=")[1];
									} else if ("1".equals(tempStr)) {
										tempStr = "颜色";
									} else if ("2".equals(tempStr)) {
										tempStr = "材质";
									}
									proposeProduct.setMainCodeLabel(tempStr);
								} else {
									failReason.add("第" + count + "笔资料-主规格名：不可为空<BR/>");
									proposeProduct = null;
									fail += 1;
								}
							}

							// AI 34 主规格项目
							if (null != proposeProduct) {
								String mainSpecName = list.get(34).trim();
								if (defaultTps.contains(mainSpecName)) {
									failReason.add("第" + count + "笔资料-主规格项目：不可为空<BR/>");
									proposeProduct = null;
									fail += 1;
								}
							}

							// AK 36 次规格名
							if (null != proposeProduct) {
								String secondSpecLabel = list.get(36).trim();
								if (!defaultTps.contains(secondSpecLabel)) {
									String tempStr = secondSpecLabel;
									if (tempStr.startsWith("3")) {
										tempStr = StringUtils.split(tempStr, "=")[1];
									} else if ("1".equals(tempStr)) {
										tempStr = "尺寸";
									} else if ("2".equals(tempStr)) {
										tempStr = "尺码";
									}
									proposeProduct.setSecondCodeLabel(tempStr);
								} else {
									failReason.add("第" + count + "笔资料-次规格名：不可为空<BR/>");
									proposeProduct = null;
									fail += 1;
								}
							}

							// AL 37 次规格项目
							if (null != proposeProduct) {
								String secondSpecName = list.get(37).trim();
								if (defaultTps.contains(secondSpecName)) {
									failReason.add("第" + count + "笔资料-次规格项目：不可为空<BR/>");
									proposeProduct = null;
									fail += 1;
								}
							}

							// AJ 35 主图细节图
							// if (null != proposeProduct) {
							// String mainSpecDetailPic = list.get(35).trim();
							// if (defaultTps.contains(mainSpecDetailPic)) {
							// proposeProduct.setProposeProductMainSpecPic("");
							// } else {
							// proposeProduct.setProposeProductMainSpecPic(mainSpecDetailPic);
							// }
							// }

							// AS 44 退换货条件
							if (null != proposeProduct) {
								String returnRule = list.get(44).trim();
								List<String> returnRuleList = new ArrayList(menuService.get("Order.ReturnRule").getOptions().keySet());
								if (returnRuleList.contains(returnRule)) {
									proposeProduct.setReturnRule(returnRule);
								} else {
									proposeProduct.setReturnRule("07_15");
								}
							}

							// try {
							if (null != proposeProduct) {
								try {
									proposeProduct.setProposeProductBatchId(batchId);
									proposeProduct.setIsDelete("N");
									proposeProduct.setDataSrc("PM");
									proposeProduct.setStepFlag(new BigDecimal("2"));
									proposeProduct.setSupplierId(supplier);

									// 台湾馆供应商
									if (null != proposeProduct) {
										if (defaultTps.contains(supplierTW)) {
											if (Arrays.asList("010000", "010001").contains(supplierTW)) {
												failReason.add("供应商为万美供应商(010000 or 010001)时，台湾馆供应商为必填<BR/>");
												proposeProduct = null;
												fail += 1;
											} else {
												proposeProduct.setTwSupplierId("");
											}
										} else {
											proposeProduct.setTwSupplierId(supplierTW);
										}
									}
									// 体积自己算
									proposeProduct.setVolume(proposeProduct.getLength().multiply(proposeProduct.getWidth()).multiply(
											proposeProduct.getHeight()));

									proposeProduct = save(proposeProduct);
									products.add(proposeProduct.getId());
									// X 23 商品特色
									if (!defaultTps.contains(list.get(23).trim())) {
										saveToFile(proposeProduct.getId(), list.get(23).trim(), "brief");
									}
									// Y 24 商品描述
									if (!defaultTps.contains(list.get(24).trim())) {
										saveToFile(proposeProduct.getId(), list.get(24).trim(), "description");
									}
									// Z 25 规格细节
									if (!defaultTps.contains(list.get(25).trim())) {
										saveToFile(proposeProduct.getId(), list.get(25).trim(), "details");
									}
									// AA 26 特殊商品购买需知
									if ("Y".equals(list.get(3).trim().toUpperCase())) {// SCM轉單，特殊購物需知必填
										if (!defaultTps.contains(list.get(26).trim())) {
											saveToFile(proposeProduct.getId(), list.get(26).trim(), "scmNotice");
										}
									}
									checkName.put(list.get(0) + "ID", proposeProduct.getId());
								} catch (Exception e) {

									e.printStackTrace();
									fail += 1;
								}

							}
						}

						if (checkName.containsKey(list.get(0)) && checkName.containsKey(list.get(0) + "ID")) {
							ProposeSpecification proposeSpecification = new ProposeSpecification();
							String mainSpecName = "";
							String secondSpecName = "";
							// AI 34 主规格项目 @ AL 37 次规格项目
							if (StringUtils.isNotBlank(list.get(34).trim()) && StringUtils.isNotBlank(list.get(37).trim())) {
								mainSpecName = StringUtils.replace(StringUtils.replace(list.get(34).trim(), "／", "/"), ",", "，");
								secondSpecName = StringUtils.replace(StringUtils.replace(list.get(37).trim(), "／", "/"), ",", "，");
								proposeSpecification.setName(mainSpecName + EcConstant.SPEC_SYMBOL + secondSpecName);
								proposeSpecification.setPrpsMainSpecName(mainSpecName);

								if (null == proposeProduct.getId()) {// 判断是否有id，没有表示同一笔主档
									proposeSpecification.setProposeProductId(checkName.get(list.get(0) + "ID"));
								} else {
									proposeSpecification.setProposeProductId(proposeProduct.getId());
								}

							} else {
								failReason.add("第" + count + "笔资料-主规格项目或次规格项目：不可为空<BR/>");
								proposeSpecification = null;
								fail += 1;
							}

							// AC 28 规格图(40*40)
							if (null != proposeSpecification) {
								String iconPics = list.get(28).trim();
								if (defaultTps.contains(iconPics)) {
									// failReason.add("第" + count + "笔资料-规格图(40*40)：不可为空<BR/>");
									// proposeSpecification = null;
									// fail += 1;
									proposeSpecification.setProposeProductIconPic("");
								} else {
									proposeSpecification.setProposeProductIconPic(iconPics);
								}
							}

							// AD 29 正方形主图(400*400)
							if (null != proposeSpecification) {
								String squarMainPic = list.get(29).trim();
								if (defaultTps.contains(squarMainPic)) {
									failReason.add("第" + count + "笔资料-正方形主图(400*400)：不可为空<BR/>");
									proposeSpecification = null;
									fail += 1;
								} else {
									proposeSpecification.setProposeProductMainImg(squarMainPic);
								}
							}

							// AE 30 长方形主图（400*530)
							if (null != proposeSpecification) {
								String rectangleMainPic = list.get(30).trim();
								if (defaultTps.contains(rectangleMainPic)) {
									failReason.add("第" + count + "笔资料-长方形主图（400*530)：不可为空<BR/>");
									proposeSpecification = null;
									fail += 1;
								} else {
									proposeSpecification.setProposeProductRMainImg(rectangleMainPic);
								}
							}

							// AJ 35 主图细节图
							if (null != proposeSpecification) {
								String mainSpecDetailPic = list.get(35).trim();
								if (defaultTps.contains(mainSpecDetailPic)) {
									proposeSpecification.setProposeProductMainSpecPic("");
								} else {
									proposeSpecification.setProposeProductMainSpecPic(mainSpecDetailPic);
								}
							}

							// AM 38 規格料號
							if (null != proposeSpecification) {
								if (defaultTps.contains(list.get(38).trim())) {
									proposeSpecification.setSpecCode("");
								} else {
									proposeSpecification.setSpecCode(list.get(38).trim());
								}
								// AN 39 規格條碼
								if (defaultTps.contains(list.get(39).trim())) {
									proposeSpecification.setBarCode("");
								} else {
									proposeSpecification.setBarCode(list.get(39).trim());
								}
								// AO 40 單筆订单最大可售數
								if (defaultTps.contains(list.get(40).trim())) {
									proposeSpecification.setMaxCountPerOrder(10);
								} else {
									proposeSpecification.setMaxCountPerOrder(Integer.parseInt(list.get(40).trim()));
								}
								// AP 41 單筆订单最小可售數
								if (defaultTps.contains(list.get(41).trim())) {
									proposeSpecification.setMinCountPerOrder(1);
								} else {
									proposeSpecification.setMinCountPerOrder(Integer.parseInt(list.get(41).trim()));
								}
								// AQ 42 安全库存量
								if (defaultTps.contains(list.get(42).trim())) {
									proposeSpecification.setSafeInventory(1);
								} else {
									proposeSpecification.setSafeInventory(Integer.parseInt(list.get(42).trim()));
								}

								// AR 43 此主规格设为封面
								if (ynTps.contains(list.get(43).trim())) {
									String isCover = list.get(43).trim();
									if ("Y".equals(isCover)) {
										if (setCover.containsKey(proposeSpecification.getProposeProductId())) {
											if (setCover.get(proposeSpecification.getProposeProductId()).equals(mainSpecName)) {
												proposeSpecification.setIsCover(isCover);
											} else {
												proposeSpecification.setIsCover("N");
											}

										} else {
											proposeSpecification.setIsCover(isCover);
											setCover.put(proposeSpecification.getProposeProductId(), proposeSpecification
													.getPrpsMainSpecName());
										}
									} else {
										proposeSpecification.setIsCover(isCover);
									}
								} else {
									proposeSpecification.setIsCover("N");
								}

								// 處理規格集合
								ProposeProduct pp = get(proposeSpecification.getProposeProductId());
								if (null != pp) {
									if (StringUtils.isBlank(pp.getMainSpecName())) {
										pp.setMainSpecName(mainSpecName);
									} else if (!Arrays.asList(StringUtils.split(pp.getMainSpecName(), ",")).contains(mainSpecName)) {
										pp.setMainSpecName(pp.getMainSpecName() + "," + mainSpecName);
									}

									if (StringUtils.isBlank(pp.getSecondSpecName())) {
										pp.setSecondSpecName(secondSpecName);
									} else if (!Arrays.asList(StringUtils.split(pp.getSecondSpecName(), ",")).contains(secondSpecName)) {
										pp.setSecondSpecName(pp.getSecondSpecName() + "," + secondSpecName);
									}
									save(pp);
								}

								if (null != proposeSpecification) {
									proposeSpecification.setSpecStatus("10");
									proposeSpecificationService.save(proposeSpecification);
									success += 1;
								}
							}
						}
					}
				} catch (Exception e) {
					e.printStackTrace();
					if (!defaultTps.contains(list.get(0).trim())) {
						failReason.add(list.get(0) + "-" + count + " 格式错误<BR/>");
						fail += 1;
					}
				}
			}
			resultList.put("countTotal", String.valueOf(countTotal));
			resultList.put("successNum", String.valueOf(success));
			resultList.put("failNum", String.valueOf(countTotal - success));
			resultList.put("failReason", failReason);
			resultList.put("readyToProduct", products);
		} catch (Exception e) {
			logger.info("exception:{}", e.getMessage());
		}
		return resultList;

	}

	@Override
	@Transactional(readOnly = false)
	public Map<String, Object> uploadExcel(List<List> excelLists, String batchId) throws CoreException {
		Map<String, Object> resultList = new HashMap<String, Object>();
		int countTotal = 0;
		int success = 0;
		int fail = 0;
		ArrayList<String> failReason = new ArrayList<String>();
		Hashtable<String, String> checkName = new Hashtable<String, String>();// 确认下一笔名称是否相同
		List<String> defaultTps = Arrays.asList("@", "＠", "");
		List<String> ynTps = Arrays.asList("Y", "N");
		List<String> scmTps = Arrays.asList("Y", "N", "B");// 采购型态(scm、po、bo)
		List<String> grapTps = Arrays.asList("R", "S");// 长形图/方形图
		try {
			countTotal = excelLists.size();
			for (List<String> list : excelLists) {
				try {
					if (list.size() > 0) {
						ProposeProduct proposeProduct = new ProposeProduct();
						if (checkName.containsKey(list.get(0)) == false) {// 判断是否在hashtable已有商品名称
							if (null != proposeProduct) {
								if (defaultTps.contains(list.get(0).trim())) {
									proposeProduct.setName("");
									checkName.put(list.get(0), list.get(0));
								} else {
									proposeProduct.setName(list.get(0).trim());
									checkName.put(list.get(0), list.get(0).trim());
								}
							}
							if (null != proposeProduct) {
								if (defaultTps.contains(list.get(1).trim())) {
									proposeProduct.setType(0);
								} else {
									proposeProduct.setType(Integer.parseInt(list.get(1).trim()));
								}
							}
							if (null != proposeProduct) {
								if (defaultTps.contains(list.get(2).trim())) {
									proposeProduct.setIsVirtual("N");
								} else {
									proposeProduct.setIsVirtual(list.get(2).trim().toUpperCase());
								}
							}
							if (null != proposeProduct) {
								if (defaultTps.contains(list.get(3).trim())) {
									proposeProduct.setProposeCategoryId(Integer.parseInt("0"));
								} else {
									proposeProduct.setProposeCategoryId(Integer.parseInt(list.get(3)));
								}
							}
							if (null != proposeProduct) {
								if (defaultTps.contains(list.get(4).trim())) {
									proposeProduct.setCost(BigDecimal.ZERO);
								} else {
									proposeProduct.setCost(new BigDecimal(list.get(4).trim()).setScale(2, BigDecimal.ROUND_HALF_UP));
								}
							}
							if (null != proposeProduct) {
								if (defaultTps.contains(list.get(5).trim())) {
									proposeProduct.setListPrice(BigDecimal.ZERO);
								} else {
									proposeProduct.setListPrice(new BigDecimal(list.get(5).trim()).setScale(2, BigDecimal.ROUND_HALF_UP));
								}
							}
							if (null != proposeProduct) {
								if (defaultTps.contains(list.get(6).trim())) {
									proposeProduct.setBrief("");
								}
							}
							if (null != proposeProduct) {
								if (defaultTps.contains(list.get(7).trim())) {
									proposeProduct.setDescription("");
								}
							}
							if (null != proposeProduct) {
								if (defaultTps.contains(list.get(8).trim())) {
									proposeProduct.setDetails("");
								}
							}
							if (null != proposeProduct) {
								if (defaultTps.contains(list.get(9).trim())) {
									proposeProduct.setBrand("");
								} else {
									try {
										proposeProduct.setBrand(list.get(9).trim());
									} catch (Exception e) {
										failReason.add(list.get(0) + "-" + list.get(26) + "-" + list.get(9) + " 格式错误<BR/>");
										fail += 1;
										proposeProduct = null;
									}
								}
							}
							if (null != proposeProduct) {
								if (defaultTps.contains(list.get(10).trim())) {
									proposeProduct.setTaxClass("");
								} else {
									try {
										proposeProduct.setTaxClass(new BigDecimal(list.get(10)).setScale(2, BigDecimal.ROUND_HALF_UP)
												.toString());
									} catch (Exception e) {
										failReason.add(list.get(0) + "-" + list.get(26) + "-" + list.get(10) + " 格式错误<BR/>");
										fail += 1;
										proposeProduct = null;
									}
								}
							}
							if (null != proposeProduct) {
								if (defaultTps.contains(list.get(11).trim())) {
									proposeProduct.setLength(BigDecimal.ZERO);
								} else {
									try {
										proposeProduct.setLength(new BigDecimal(list.get(11).trim()).setScale(2, BigDecimal.ROUND_HALF_UP));
									} catch (Exception e) {
										failReason.add(list.get(0) + "-" + list.get(26) + "-" + list.get(11) + " 格式错误<BR/>");
										fail += 1;
										proposeProduct = null;
									}
								}
							}
							if (null != proposeProduct) {
								if (defaultTps.contains(list.get(12).trim())) {
									proposeProduct.setWidth(BigDecimal.ZERO);
								} else {
									proposeProduct.setWidth(new BigDecimal(list.get(12).trim()).setScale(2, BigDecimal.ROUND_HALF_UP));
								}
							}
							if (null != proposeProduct) {
								if (defaultTps.contains(list.get(13).trim())) {
									proposeProduct.setHeight(BigDecimal.ZERO);
								} else {
									proposeProduct.setHeight(new BigDecimal(list.get(13).trim()).setScale(2, BigDecimal.ROUND_HALF_UP));
								}
							}
							if (null != proposeProduct) {
								if (defaultTps.contains(list.get(14).trim())) {
									proposeProduct.setWeight(BigDecimal.ZERO);
								} else {
									proposeProduct.setWeight(new BigDecimal(list.get(14).trim()).setScale(2, BigDecimal.ROUND_HALF_UP));
								}
							}
							if (null != proposeProduct) {
								if (defaultTps.contains(list.get(15).trim())) {
									proposeProduct.setVolume(BigDecimal.ZERO);
								} else {
									if (null != proposeProduct && defaultTps.contains(list.get(11).trim())
											|| defaultTps.contains(list.get(12).trim()) || defaultTps.contains(list.get(13).trim())) {
										proposeProduct.setVolume(BigDecimal.ZERO);
									} else {
										proposeProduct.setVolume((new BigDecimal(list.get(11).trim()).multiply(new BigDecimal(list.get(12)
												.trim())).multiply(new BigDecimal(list.get(13).trim()))).setScale(2, BigDecimal.ROUND_HALF_UP));
									}
								}
							}
							if (null != proposeProduct) {
								if (defaultTps.contains(list.get(16).trim())) {
									proposeProduct.setUnit("");
								} else {
									proposeProduct.setUnit(list.get(16).trim());
								}
							}
							if (null != proposeProduct) {
								if (defaultTps.contains(list.get(17).trim())) {
									proposeProduct.setLeadTime(0);
								} else {
									try {
										proposeProduct.setLeadTime(Integer.parseInt(list.get(17).trim()));
									} catch (Exception e) {
										failReason.add(list.get(0) + "-" + list.get(26) + "-" + list.get(17) + " 格式错误<BR/>");
										proposeProduct = null;
									}
								}
							}
							if (null != proposeProduct) {
								if (defaultTps.contains(list.get(18).trim())) {
									proposeProduct.setIsDeliverdTolerance("N");
								} else {
									try {
										if (null != proposeProduct && !ynTps.contains(list.get(18).trim().toUpperCase())) {
											proposeProduct.setIsDeliverdTolerance("N");
										} else {
											proposeProduct.setIsDeliverdTolerance(list.get(18).trim().toUpperCase());
										}
									} catch (Exception e) {
									}
								}
							}
							if (null != proposeProduct) {
								if (defaultTps.contains(list.get(19).trim())) {
									proposeProduct.setMinPoQuantity(0);
								} else {
									try {
										proposeProduct.setMinPoQuantity(Integer.parseInt(list.get(19).trim()));
									} catch (Exception e) {
										proposeProduct = null;
									}
								}
							}
							if (null != proposeProduct) {
								if (defaultTps.contains(list.get(20).trim())) {
									proposeProduct.setMaxPoQuantity(10);
								} else {
									proposeProduct.setMaxPoQuantity(Integer.parseInt(list.get(20).trim()));
								}
							}
							if (null != proposeProduct) {
								if (defaultTps.contains(list.get(21).trim())) {
									proposeProduct.setTemperatureType(0);
								} else {
									proposeProduct.setTemperatureType(Integer.parseInt(list.get(21).trim()));
								}
							}
							if (null != proposeProduct) {
								if (defaultTps.contains(list.get(22).trim())) {
									proposeProduct.setIsReturnPickup("Y");
								} else {
									if (!ynTps.contains(list.get(22).trim().toUpperCase())) {
										proposeProduct.setIsReturnPickup("Y");
									} else {
										proposeProduct.setIsReturnPickup(list.get(22).trim().toUpperCase());
									}
								}
							}
							if (null != proposeProduct) {
								if (defaultTps.contains(list.get(23).trim())) {
									proposeProduct.setIsReturnable("Y");
								} else {
									if (!ynTps.contains(list.get(23).trim().toUpperCase())) {
										proposeProduct.setIsReturnable("Y");
									} else {
										proposeProduct.setIsReturnable(list.get(23).trim().toUpperCase());
									}
								}
							}
							if (null != proposeProduct) {
								if (defaultTps.contains(list.get(24).trim())) {
									proposeProduct.setPmId("");
								} else {
									proposeProduct.setPmId(list.get(24).trim().toLowerCase());
								}
							}
							if (null != proposeProduct) {
								if (defaultTps.contains(list.get(25).trim())) {
									proposeProduct.setMeno("");
								} else {
									proposeProduct.setMeno(list.get(25).trim());
								}
							}
							if (null != proposeProduct) {
								if (defaultTps.contains(list.get(31).trim())) {
									proposeProduct.setProductKeyword("");
								} else {
									proposeProduct.setProductKeyword(list.get(31).trim());
								}
							}
							if (null != proposeProduct) {
								if (defaultTps.contains(list.get(32).trim())) {
									proposeProduct.setProposeProductMainImg("");
								} else {
									proposeProduct.setProposeProductMainImg(list.get(32).trim());
								}
							}
							if (null != proposeProduct) {
								if (defaultTps.contains(list.get(33).trim())) {
									proposeProduct.setProposeProductDescImg("");
								} else {
									proposeProduct.setProposeProductDescImg(list.get(33).trim());
								}
							}
							if (null != proposeProduct) {
								if (defaultTps.contains(list.get(34).trim())) {
									proposeProduct.setDeliverGeoRange((""));
								} else {
									if (list.get(34).trim().length() > 5) {
										failReason.add(list.get(0) + "-" + list.get(34) + " 格式错误<BR/>");
										fail += 1;
										proposeProduct = null;
									} else {
										proposeProduct.setDeliverGeoRange(list.get(34).trim());
									}
								}
							}
							if (null != proposeProduct) {
								if (defaultTps.contains(list.get(35).trim())) {
									proposeProduct.setSupplierId("");
								} else {
									proposeProduct.setSupplierId(converSupid(list.get(35).trim()));
								}
							}
							if (null != proposeProduct) {
								if (defaultTps.contains(list.get(36).trim())) {
									proposeProduct.setProposeProductPrdtImg((""));
								} else {
									proposeProduct.setProposeProductPrdtImg(list.get(36).trim());
								}
							}
							if (null != proposeProduct) {
								if (defaultTps.contains(list.get(37).trim())) {
									proposeProduct.setPrpsShelfPrice(BigDecimal.ZERO);
								} else {
									proposeProduct.setPrpsShelfPrice(new BigDecimal(list.get(37).trim()).setScale(2,
											BigDecimal.ROUND_HALF_UP));
								}
							}
							if (null != proposeProduct) {
								if (scmTps.contains(list.get(38).trim().toUpperCase())) {
									proposeProduct.setIsScm(list.get(38).trim().toUpperCase());
								} else {
									proposeProduct.setIsScm("N");
								}
							}
							// 预购商品的采购模式不可为入仓
							if (null != proposeProduct && 1 == proposeProduct.getType()
									&& "N".equalsIgnoreCase(proposeProduct.getIsScm())) {
								failReason.add(list.get(0) + "-预购商品的采购模式不可为入仓<BR/>");
								proposeProduct = null;
								fail += 1;
							}
							// 效期管理分类
							if (null != proposeProduct) {
								if (defaultTps.contains(list.get(41).trim())) {
									proposeProduct.setExpireManage("EM01");
								} else {
									proposeProduct.setExpireManage(list.get(41).trim().toUpperCase());
								}
							}
							// 是否需要防拆包材
							if (null != proposeProduct) {
								if (defaultTps.contains(list.get(42).trim())) {
									proposeProduct.setIsAntiUnpacking("N");
								} else {
									proposeProduct.setIsAntiUnpacking(list.get(42).trim().toUpperCase());
								}
							}
							// 長方圖
							if (null != proposeProduct) {
								if (defaultTps.contains(list.get(43).trim())) {
									proposeProduct.setProposeProductRMainImg("");
								} else {
									proposeProduct.setProposeProductRMainImg(list.get(43).trim());
								}
							}
							// 長方圖R/正方圖S
							if (null != proposeProduct) {
								if (!grapTps.contains(list.get(44).trim())) {
									proposeProduct.setGraphicType("S");
								} else {
									proposeProduct.setGraphicType(list.get(44).trim());
								}
							}
							// 是否使用"订购后几日出货"?
							if (null != proposeProduct) {
								if (defaultTps.contains(list.get(45).trim())) {
									proposeProduct.setUseDeliveryDay("N");
								} else if (ynTps.contains(list.get(45).trim())) {
									proposeProduct.setUseDeliveryDay(list.get(45).trim().toUpperCase());
								} else {
									failReason.add(list.get(45) + "-是否使用\"订购后几日出货\"：" + list.get(45) + " 提報是否使用订购后几日出货－格式错误<BR/>");
									proposeProduct = null;
									fail += 1;
								}
							}
							// 订购后几日出货?
							if (null != proposeProduct) {
								if (defaultTps.contains(list.get(46).trim())) {
									proposeProduct.setDeliveryDay(0);
								} else if (NumberUtils.isNumber(list.get(46).trim())) {
									proposeProduct.setDeliveryDay(Integer.parseInt(list.get(46).trim()));
								} else {
									failReason.add(list.get(0) + "-订购后几日出货：" + list.get(46) + " 提報订购后几日出货－格式错误<BR/>");
									proposeProduct = null;
									fail += 1;
								}
							}
							// 台湾馆供应商
							if (null != proposeProduct) {
								if (defaultTps.contains(list.get(47).trim())) {
									if (Arrays.asList("010000", "010001").contains(list.get(35).trim())) {
										failReason.add(list.get(0) + "-供应商为万美供应商(010000 or 010001)时，台湾馆供应商为必填<BR/>");
										proposeProduct = null;
										fail += 1;
									} else {
										proposeProduct.setTwSupplierId("");
									}
								} else {
									proposeProduct.setTwSupplierId(list.get(47).trim());
								}
							}
							// try {
							if (null != proposeProduct) {
								try {
									proposeProduct.setProposeProductBatchId(batchId);
									proposeProduct.setIsDelete("N");
									proposeProduct = save(proposeProduct);
									if (!defaultTps.contains(list.get(6).trim())) {
										saveToFile(proposeProduct.getId(), list.get(6).trim(), "brief");
									}
									if (!defaultTps.contains(list.get(7).trim())) {
										saveToFile(proposeProduct.getId(), list.get(7).trim(), "description");
									}
									if (!defaultTps.contains(list.get(8).trim())) {
										saveToFile(proposeProduct.getId(), list.get(8).trim(), "details");
									}
									if ("Y".equals(list.get(38).trim().toUpperCase())) {// SCM轉單，特殊購物需知必填
										if (!defaultTps.contains(list.get(40).trim())) {
											saveToFile(proposeProduct.getId(), list.get(40).trim(), "scmNotice");
										}
									}
									checkName.put(list.get(0) + "ID", proposeProduct.getId());
								} catch (Exception e) {
									e.printStackTrace();
									fail += 1;
								}

							}
						}

						if (checkName.containsKey(list.get(0)) == true) {
							ProposeSpecification proposeSpecification = new ProposeSpecification();
							if (defaultTps.contains(list.get(26).trim())) {
								proposeSpecification.setName("");
							} else {
								proposeSpecification.setName(list.get(26).trim());
							}
							if (null == proposeProduct.getId()) {// 判断是否有id，没有表示同一笔主档
								proposeSpecification.setProposeProductId(checkName.get(list.get(0) + "ID"));
							} else {
								proposeSpecification.setProposeProductId(proposeProduct.getId());
							}
							if (defaultTps.contains(list.get(27).trim())) {
								proposeSpecification.setSpecCode("");
							} else {
								proposeSpecification.setSpecCode(list.get(27).trim());
							}
							if (defaultTps.contains(list.get(28).trim())) {
								proposeSpecification.setBarCode("");
							} else {
								proposeSpecification.setBarCode(list.get(28).trim());
							}
							if (defaultTps.contains(list.get(29).trim())) {
								proposeSpecification.setMaxCountPerOrder(10);
							} else {
								proposeSpecification.setMaxCountPerOrder(Integer.parseInt(list.get(29).trim()));
							}
							if (defaultTps.contains(list.get(30).trim())) {
								proposeSpecification.setMinCountPerOrder(1);
							} else {
								proposeSpecification.setMinCountPerOrder(Integer.parseInt(list.get(30).trim()));
							}
							if (defaultTps.contains(list.get(39).trim())) {
								proposeSpecification.setSpecStatus("10");
							} else {
								// 规格状态
								if (Arrays.asList("10", "20", "30", "40").contains(list.get(39).trim())) {
									proposeSpecification.setSpecStatus(list.get(39).trim());
								} else {
									failReason.add(list.get(0) + "-规格状态：" + list.get(39) + " 提報規格－格式错误<BR/>");
									proposeSpecification.setSpecStatus(list.get(39).trim());
									proposeSpecification = null;
									fail += 1;
								}
							}
							if (null != proposeSpecification) {
								proposeSpecificationService.save(proposeSpecification);
								success += 1;
							}
						}
					}
				} catch (Exception e) {
					logger.info(e.toString());
					if (!defaultTps.contains(list.get(0).trim())) {
						failReason.add(list.get(0) + "-" + list.get(26) + " 格式错误<BR/>");
						fail += 1;
					}
				}
			}
			resultList.put("countTotal", String.valueOf(countTotal));
			resultList.put("successNum", String.valueOf(success));
			resultList.put("failNum", String.valueOf(fail));
			resultList.put("failReason", failReason);
		} catch (Exception e) {
			logger.info("exception:{}", e.getMessage());
		}
		return resultList;
	}

	@Override
	public List<ProposeProduct> getList(int firstResult, int maxResults, CommonCriteria criteria, String[] sortOrder)
			throws CoreException {
		CriteriaInRlike critInRlike = mergeCritInRlike(criteria, "category.categoryPath");// 分線維護權限檢核
		return super.getList(firstResult, maxResults, critInRlike, sortOrder);
	}

	@Override
	public Number getListSize(CommonCriteria criteria) throws CoreException {
		CriteriaInRlike critInRlike = mergeCritInRlike(criteria, "category.categoryPath");// 分線維護權限檢核
		return super.getListSize(critInRlike);
	}

	/**
	 * 分線維護權限檢核
	 * 
	 * @param commcriteria
	 * @return
	 */
	CriteriaInRlike mergeCritInRlike(CommonCriteria criteria, String tableParam) {
		CriteriaInRlike critInRlike = null;
		User user = AdminHelper.getUser();
		if (null != user) {
			if (null == criteria) {
				criteria = new CommonCriteria();
			}
			critInRlike = new CriteriaInRlike(criteria);
			// 若 user 是 PM部門, 檢核 product.category 的 orgnizationPath 是否合乎規定
			List<Serializable> prs = new ArrayList<Serializable>();
			for (UserPermission up : user.getPermissions(Type.CATEGORY).values()) {
				prs.add(up.getPermissionCode() + STRING_SYMBOL);
			}
			critInRlike.addInRlike(tableParam, prs);
		} else {
			if (null == criteria) {
				criteria = new CommonCriteria();
			}
			critInRlike = new CriteriaInRlike(criteria);
		}
		return critInRlike;
	}

	@Override
	public ProposeProduct get(Serializable oid) throws CoreException {
		ProposeProduct product = super.get(oid);
		if (null != product) {
			Category productCategory = categoryService.get(new Long(product.getProposeCategoryId()));
			// 分線維護權限檢核
			User user = AdminHelper.getUser();
			if (null != user) {
				// 若 user 是 PM部門, 檢核 product.category 的 orgnizationPath 是否合乎規定
				if (user.getPermissions(Type.CATEGORY).size() > 0) {
					if (StringUtils.isNotBlank(productCategory.getRootId())) {
						if (!user.getPermissions(Type.CATEGORY).keySet().contains("CATEGORY_" + productCategory.getRootId())) {
							product = null;
						}
					}
				}
			}
		}
		return product;
	}
}